package com.itheima.service.impl;


import com.itheima.dao.AccountDao;
import com.itheima.service.AccountService;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;

public class AccountServiceImpl implements AccountService {


    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void transfer(String outName, String inName, Double money) {
        /*创建事务管理器,并且为事务管理器配置相同的数据源*/
        DataSourceTransactionManager ptm=new DataSourceTransactionManager(dataSource);
        //创建事务定义对象
        TransactionDefinition td=new DefaultTransactionDefinition();
        //创建事务状态对象，来控制事务
        TransactionStatus ts=ptm.getTransaction(td);

        accountDao.inMoney(outName,money);

        accountDao.outMoney(inName,money);

        ptm.commit(ts);
    }

}
